//
// Created by dlr123 on 2021/11/27.
//

#include "uart_conf.h"

uint8_t Char_Num;
UartState_TypeDef work_flag;

void UART_WORK_Init()
{
    __HAL_UART_DISABLE_IT(&huart4, UART_IT_RXNE);
    __HAL_UART_DISABLE_IT(&huart4, UART_IT_TC);
    HAL_UART_Receive_DMA(&huart4, RxBuffer1, 1);
    work_flag = UART_FREE;
    Char_Num = 0;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

    if (RxBuffer1[0] == ';') {
        if (Char_Num != 0) {
            Rx_Uart_Data[Char_Num] = '\n';
            HAL_UART_Transmit(huart, Rx_Uart_Data, Char_Num + 1, HAL_MAX_DELAY);
            work_flag = UART_BUSY;
        } else {
            printf("the current instruct is empty!\n");
        }
//        Char_Num = 0;
    } else {
        Rx_Uart_Data[Char_Num] = RxBuffer1[0];
        Char_Num++;
        if (Char_Num >= MAX_BUFFER_SIZE) {
            printf("the instruct is so long,and it cannot be show!\n");
            Char_Num = 0;
        }
    }
    if(work_flag == UART_FREE){
        __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
    }
}

void on_UART_IDLE(UART_HandleTypeDef *huart) {
    if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_IDLE) == RESET)
        return;
    __HAL_UART_CLEAR_IDLEFLAG(huart);
    __HAL_UART_DISABLE_IT(huart, UART_IT_IDLE);
    HAL_UART_Receive_DMA(huart, RxBuffer1, 1);
}


void Process_RxData()
{
    if(work_flag == UART_BUSY)
    {
        UNUSED(work_flag);

        work_flag = UART_FREE;
        Char_Num = 0;
        HAL_UART_Receive_DMA(&huart4, RxBuffer1, 1);
    }
}

